package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.project.ProjectSettings;
import info.thereisonlywe.core.toolkit.StringToolkit;
import info.thereisonlywe.onenesspathfinder.controllers.RecitationController;
import info.thereisonlywe.quran.Quran;
import info.thereisonlywe.salat.recitation.reciter.ReciterList;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import com.jidesoft.swing.ComboBoxSearchable;
import com.jidesoft.swing.SearchableUtils;

public class NavigationBar extends JPanel {

	protected static final JComboBox<String> comboBox_Section = new JComboBox<String>();
	protected static final JComboBox<Integer> comboBox_Verse = new JComboBox<Integer>();
	protected final JButton button_Previous = new JButton();
	protected final JButton button_Next = new JButton();
	protected final JButton button_Recite = new JButton();
	protected final JButton button_Stop = new JButton();
	protected final JButton button_Speaker = new JButton();
	protected final JPopupMenu popup_Recite = new JPopupMenu();
	protected final JPopupMenu popup_Speaker = new JPopupMenu();
	protected final JToggleButton button_Search = new JToggleButton();
	protected final JToggleButton button_ScratchPanel = new JToggleButton();
	protected final JToggleButton button_Bookmarks = new JToggleButton();
	protected final JPanel recitationSettings = new JPanel();
	private final JPanel panel_Windows = new JPanel();
	private final JPanel panel_Seek = new JPanel();
	protected static String[] sectionDetails = new String[Quran.SECTION_COUNT];

	public NavigationBar()
	{
		super();
		initComponents();
		initListeners();
	}

	private void initComponents()
	{
		setLayout(new BorderLayout());

		panel_Windows.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 3));
		panel_Seek.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 3));

		add(panel_Windows, BorderLayout.WEST);
		add(panel_Seek, BorderLayout.CENTER);

		comboBox_Section.setFont(new Font("Dialog", Font.BOLD, 13));
		comboBox_Section.setModel(new DefaultComboBoxModel<String>(Quran
				.getSectionNames(ProjectSettings.getLocale().getLanguage())));
		comboBox_Section.getInputMap(
				JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
				KeyStroke.getKeyStroke("DOWN"), "none");
		comboBox_Section.getInputMap(
				JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
				KeyStroke.getKeyStroke("UP"), "none");
		comboBox_Section.getInputMap(
				JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
				KeyStroke.getKeyStroke("RIGHT"), "none");
		comboBox_Section.getInputMap(
				JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
				KeyStroke.getKeyStroke("LEFT"), "none");
		comboBox_Section.setEditable(false);
		new ComboBoxSearchable(comboBox_Section)
		{
			@Override
			protected boolean compare(String text, String searchingText)
			{
				if (Character.isDigit(searchingText.charAt(0)))
				{
					return super.compare(text, searchingText);
				}
				else
				{
					text = text.replace("-", "");
					text = text.replace("'", "");
					text = StringToolkit.removeDiacritics(text);

					searchingText = searchingText.replace("-", "");
					searchingText = searchingText.replace("'", "");
					searchingText = StringToolkit
							.removeDiacritics(searchingText);
					return super.compare(text, "*" + searchingText);
				}
			}
		};
		panel_Seek.add(comboBox_Section);

		comboBox_Verse
				.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
						KeyStroke.getKeyStroke("DOWN"), "none");
		comboBox_Verse
				.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
						KeyStroke.getKeyStroke("UP"), "none");
		comboBox_Verse
				.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
						KeyStroke.getKeyStroke("RIGHT"), "none");
		comboBox_Verse
				.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
						KeyStroke.getKeyStroke("LEFT"), "none");
		comboBox_Verse.setFont(new Font("Dialog", Font.BOLD, 13));
		comboBox_Verse.setModel(new DefaultComboBoxModel<Integer>(
				new Integer[] { 1, 2, 3, 4, 5, 6, 7 }));
		comboBox_Verse.setEditable(false);
		SearchableUtils.installSearchable(comboBox_Verse);
		panel_Seek.add(comboBox_Verse);

		button_Previous
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/prev.png")));
		button_Previous.setFocusPainted(false);
		button_Previous.setMargin(new Insets(0, 0, 0, 0));
		button_Previous.setContentAreaFilled(false);
		button_Previous.setBorderPainted(false);
		button_Previous.setOpaque(false);
		button_Previous.putClientProperty("JComponent.sizeVariant", "mini");
		button_Previous.setPreferredSize(new Dimension(40, 30));
		button_Previous.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),
				"none");
		button_Previous.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
				"none");
		panel_Seek.add(button_Previous);

		button_Next
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/next.png")));
		button_Next.setFocusPainted(false);
		button_Next.setMargin(new Insets(0, 0, 0, 0));
		button_Next.setContentAreaFilled(false);
		button_Next.setBorderPainted(false);
		button_Next.setOpaque(false);
		button_Next.putClientProperty("JComponent.sizeVariant", "mini");
		button_Next.setPreferredSize(new Dimension(40, 30));
		button_Next.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "none");
		button_Next.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "none");
		panel_Seek.add(button_Next);

		button_Stop
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/stop.png")));
		button_Stop.setFocusPainted(false);
		button_Stop.setMargin(new Insets(0, 0, 0, 0));
		button_Stop.setContentAreaFilled(false);
		button_Stop.setBorderPainted(false);
		button_Stop.setOpaque(false);
		button_Stop.putClientProperty("JComponent.sizeVariant", "mini");
		button_Stop.setPreferredSize(new Dimension(40, 30));
		button_Stop.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "none");
		button_Stop.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "none");
		panel_Seek.add(button_Stop);

		JMenuItem item;

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "ReciteVerse"));
		item.setMargin(UIConstants.MARGIN);
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				RecitationController.reciteVerse();
			}
		});
		popup_Recite.add(item);

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "ReciteSection"));
		item.setMargin(UIConstants.MARGIN);
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				RecitationController.reciteSection();
			}
		});
		popup_Recite.add(item);

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT,
				"ArrangedRecitation"));
		item.setMargin(UIConstants.MARGIN);
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				RecitationDialog.showDialog();
			}
		});
		popup_Recite.add(item);

		button_Recite
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/play.png")));
		button_Recite.setFocusPainted(false);
		button_Recite.setMargin(new Insets(0, 0, 0, 0));
		button_Recite.setContentAreaFilled(false);
		button_Recite.setBorderPainted(false);
		button_Recite.setOpaque(false);
		button_Recite.putClientProperty("JComponent.sizeVariant", "mini");
		button_Recite.setPreferredSize(new Dimension(40, 30));
		button_Recite.getInputMap()
				.put(KeyStroke.getKeyStroke("SPACE"), "none");
		button_Recite.getInputMap()
				.put(KeyStroke.getKeyStroke("ENTER"), "none");
		panel_Seek.add(button_Recite);

		JMenu item2;
		JRadioButtonMenuItem item3;

		item2 = new JMenu(ProjectLocalization.getString(
				ProjectConstants.SALAT_PROJECT, "Reciter.Quran"));

		String[] reciterNames = ReciterList.getQuranReciterNames();

		String t = UIDelegate.quranReciter.getName();

		ButtonGroup bgX = new ButtonGroup();

		for (int i = 0; i < reciterNames.length; i++)
		{
			final String s = reciterNames[i];
			item3 = new JRadioButtonMenuItem(s);
			if (t != null && s.equals(t)) item3.setSelected(true);
			item3.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					UIDelegate.quranReciter = ReciterList.getReciterByName(s);
					RecitationController.getImam().resetReciters(
							UIDelegate.quranReciter,
							RecitationController.getImam().getTextReciter());
				}
			});
			bgX.add(item3);
			item2.add(item3);
		}

		popup_Speaker.add(item2);

		item2 = new JMenu(ProjectLocalization.getString(
				ProjectConstants.SALAT_PROJECT, "Reciter.Text"));

		t = UIDelegate.textReciter == null ? null : UIDelegate.textReciter
				.getName();
		reciterNames = ReciterList.getTextReciterNames();

		ButtonGroup bg = new ButtonGroup();

		item3 = new JRadioButtonMenuItem("-");
		if (t == null) item3.setSelected(true);
		item3.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				UIDelegate.textReciter = null;
				RecitationController.getImam().resetReciters(
						RecitationController.getImam().getReciter(), null);
			}
		});
		bg.add(item3);
		item2.add(item3);

		String[] specs = ReciterList.getTextReciterSpecifications();

		for (int i = 0; i < reciterNames.length; i++)
		{
			final String s = reciterNames[i];
			item3 = new JRadioButtonMenuItem(s);
			item3.setToolTipText(specs[i]);
			if (t != null && s.equals(t)) item3.setSelected(true);
			item3.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					UIDelegate.textReciter = ReciterList.getReciterByName(s);
					RecitationController.getImam().resetReciters(
							RecitationController.getImam().getReciter(),
							UIDelegate.textReciter);
				}
			});
			bg.add(item3);
			item2.add(item3);
		}

		popup_Speaker.add(item2);

		item = new JMenuItem("+ "
				+ ProjectLocalization.getString(ProjectConstants.CORE_PROJECT,
						"Volume"));
		item.setAccelerator(KeyStroke.getKeyStroke("control UP"));
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				UIDelegate.increaseVolume();
			}
		});

		popup_Speaker.add(item);

		item = new JMenuItem("- "
				+ ProjectLocalization.getString(ProjectConstants.CORE_PROJECT,
						"Volume"));
		item.setAccelerator(KeyStroke.getKeyStroke("control DOWN"));
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				UIDelegate.decreaseVolume();
			}
		});

		popup_Speaker.add(item);

		final JCheckBoxMenuItem item4 = new JCheckBoxMenuItem(
				ProjectLocalization.getString(ProjectConstants.SALAT_PROJECT,
						"Loop"));
		item4.setSelected(UIDelegate.loop);
		item4.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				RecitationController.getImam().setLoop(item4.isSelected());
				UIDelegate.loop = item4.isSelected();
			}
		});

		popup_Speaker.add(item4);
		popup_Speaker.addSeparator();

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT,
				"AudioPackageManagement"));
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				AudioPackageManagementDialog.showDialog();
			}
		});

		popup_Speaker.add(item);

		button_Speaker
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/speaker.png")));
		button_Speaker.setFocusPainted(false);
		button_Speaker.setMargin(new Insets(0, 0, 0, 0));
		button_Speaker.setContentAreaFilled(false);
		button_Speaker.setBorderPainted(false);
		button_Speaker.setOpaque(false);
		button_Speaker.putClientProperty("JComponent.sizeVariant", "mini");
		button_Speaker.setPreferredSize(new Dimension(40, 30));
		button_Speaker.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),
				"none");
		button_Speaker.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
				"none");
		panel_Seek.add(button_Speaker);

		button_Search
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/search.png")));
		button_Search.setFocusPainted(false);
		button_Search.setMargin(new Insets(0, 0, 0, 0));
		button_Search.setContentAreaFilled(false);
		button_Search.setBorderPainted(false);
		button_Search.setOpaque(false);
		button_Search.putClientProperty("JComponent.sizeVariant", "mini");
		button_Search.setPreferredSize(new Dimension(40, 30));
		button_Search.getInputMap()
				.put(KeyStroke.getKeyStroke("SPACE"), "none");
		button_Search.getInputMap()
				.put(KeyStroke.getKeyStroke("ENTER"), "none");
		panel_Windows.add(button_Search);

		button_Bookmarks
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/bookmarks.png")));
		button_Bookmarks.setFocusPainted(false);
		button_Bookmarks.setMargin(new Insets(0, 0, 0, 0));
		button_Bookmarks.setContentAreaFilled(false);
		button_Bookmarks.setBorderPainted(false);
		button_Bookmarks.setOpaque(false);
		button_Bookmarks.putClientProperty("JComponent.sizeVariant", "mini");
		button_Bookmarks.setPreferredSize(new Dimension(40, 30));
		button_Bookmarks.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),
				"none");
		button_Bookmarks.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
				"none");
		panel_Windows.add(button_Bookmarks);

		button_ScratchPanel
				.setIcon(new ImageIcon(
						NavigationBar.class
								.getResource("/info/thereisonlywe/onenesspathfinder/resources/icons/scratchpad.png")));
		button_ScratchPanel.setFocusPainted(false);
		button_ScratchPanel.setMargin(new Insets(0, 0, 0, 0));
		button_ScratchPanel.setContentAreaFilled(false);
		button_ScratchPanel.setBorderPainted(false);
		button_ScratchPanel.setOpaque(false);
		button_ScratchPanel.putClientProperty("JComponent.sizeVariant", "mini");
		button_ScratchPanel.setPreferredSize(new Dimension(40, 30));
		button_ScratchPanel.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),
				"none");
		button_ScratchPanel.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
				"none");
		panel_Windows.add(button_ScratchPanel);

		setPreferredSize(new Dimension(
				MainFrame.frame.getPreferredSize().width, 35));
	}

	private void initListeners()
	{

		button_ScratchPanel.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (button_ScratchPanel.isSelected())
				{
					MainFrame.view_Windows.mountScratchPanel();
				}
				else
				{
					MainFrame.view_Windows.unmountScratchPanel();
				}
			}
		});

		button_Search.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (button_Search.isSelected())
				{
					MainFrame.view_Windows.mountSearchContainer();
				}
				else
				{
					MainFrame.view_Windows.unmountSearchContainer();
				}
			}
		});

		button_Bookmarks.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (button_Bookmarks.isSelected())
				{
					MainFrame.view_Windows.mountBookmarkPane();
				}
				else
				{
					MainFrame.view_Windows.unmountBookmarkPane();
				}
			}
		});

		button_Speaker.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mousePressed(MouseEvent e)
			{
				popup_Speaker.show(e.getComponent(), e.getX(),
						button_Speaker.getLocation().y + 25);
			}
		});

		comboBox_Section.addPopupMenuListener(new PopupMenuListener()
		{
			@Override
			public void popupMenuCanceled(PopupMenuEvent e)
			{
			}

			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
			{
				UIDelegate.setOpenDialog(false);
				if (!UIDelegate.isBrowserLocked()
						|| RecitationController.getImam().isPaused())
				{
					UIDelegate.refreshSectionAndResetVerse();
				}
				else
				{
					comboBox_Section.setSelectedIndex(UIDelegate
							.getDisplayedVerse().getSectionNumber() - 1);
				}
			}

			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e)
			{
				UIDelegate.setOpenDialog(true);
			}
		});

		comboBox_Verse.addPopupMenuListener(new PopupMenuListener()
		{
			@Override
			public void popupMenuCanceled(PopupMenuEvent e)
			{
			}

			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
			{
				UIDelegate.setOpenDialog(false);
				UIDelegate.refreshVerse();
			}

			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e)
			{
				UIDelegate.setOpenDialog(true);
			}
		});

		button_Stop.addActionListener(new ActionListener()

		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				RecitationController.getImam().terminate();
				if (!RecitationController.getImam().isAlive())
				{
					UIDelegate.disEngageBrowserLock();
				}
			}

		});

		button_Recite.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mousePressed(MouseEvent e)
			{
				if (RecitationController.getImam().isPaused())
				{
					RecitationController.getImam().revive();
				}
				else
				{

					if (RecitationController.getImam().isAlive())
					{
						popup_Recite.getComponent(0).setEnabled(false);
						popup_Recite.getComponent(1).setEnabled(false);
						popup_Recite.getComponent(2).setEnabled(false);
					}
					else
					{
						popup_Recite.getComponent(0).setEnabled(true);
						popup_Recite.getComponent(1).setEnabled(true);
						popup_Recite.getComponent(2).setEnabled(true);
					}
					popup_Recite.show(e.getComponent(), e.getX(),
							button_Recite.getLocation().y + 25);
				}
			}
		});

		button_Next.addMouseListener(new MouseAdapter()
		{
			private Timer t;
			private boolean longPress = false;

			@Override
			public void mousePressed(MouseEvent arg0)
			{
				if (t == null)
				{
					t = new Timer();
				}
				t.schedule(new TimerTask()
				{
					@Override
					public void run()
					{
						longPress = true;
						UIDelegate.seekToNextSection();
					}
				}, 1000, 333);
			}

			@Override
			public void mouseReleased(MouseEvent arg0)
			{
				if (t != null)
				{
					if (!longPress)
					{
						UIDelegate.seekToNextVerse();
						UIDelegate.refreshVerse();
					}
					else
					{
						UIDelegate.refreshSectionAndResetVerse();
					}
					longPress = false;
					t.cancel();
					t = null;
				}
			}
		});

		button_Previous.addMouseListener(new MouseAdapter()
		{
			private Timer t;
			private boolean longPress = false;

			@Override
			public void mousePressed(MouseEvent arg0)
			{
				if (t == null)
				{
					t = new Timer();
				}
				t.schedule(new TimerTask()
				{
					@Override
					public void run()
					{
						longPress = true;
						UIDelegate.seekToPreviousSection();
					}
				}, 1000, 333);
			}

			@Override
			public void mouseReleased(MouseEvent arg0)
			{
				if (t != null)
				{
					if (!longPress)
					{
						UIDelegate.seekToPreviousVerse();
						UIDelegate.refreshVerse();
					}
					else
					{
						UIDelegate.refreshSectionAndResetVerse();
					}
					longPress = false;
					t.cancel();
					t = null;
				}
			}
		});
	}
}
